

namespace Stee.CAP8.MessageBus.Kafka.Utility
{
    using System;
    using Stee.CAP8.MessageBus.Interface.Models;
    using Stee.CAP8.MessageBus.Kafka.ResourceLib;

    public static class ValidateHelper
    {
        /// <summary>
        /// Validate topic
        /// </summary>
        /// <param name="topicName">Topic name</param>
        /// <returns>Return TRUE if valid else FALSE</returns>
        public static bool ValidateTopic(TopicName topicName)
        {
            string topic = topicName?.Name.Trim();

            if (string.IsNullOrEmpty(topic))
            {
                throw new ArgumentNullException($"{Kafka_General.KAFKA_NULLEMPTY_MESSAGE_TOPIC}: {nameof(topicName)}");
            }

            return true;
        }

        /// <summary>
        /// Validate group ID
        /// </summary>
        /// <param name="groupID">Group ID</param>
        /// <returns>Return TRUE if valid else FALSE</returns>
        public static bool ValidateGroup(string groupID)
        {
            string grp = groupID?.Trim();

            if (string.IsNullOrEmpty(grp))
            {
                throw new ArgumentNullException($"{Kafka_General.KAFKA_NULLEMPTY_GROUPID}: {nameof(groupID)}");
            }

            return true;
        }

        /// <summary>
        /// Validate message
        /// </summary>
        /// <typeparam name="TMessage">Message data type</typeparam>
        /// <param name="message">Published or incoming subscribed message</param>
        /// <returns>Return TRUE if valid else FALSE</returns>
        public static bool ValidateMessage<TMessage>(TMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException($"{Kafka_Producer.KAFKA_WARN_NULL_MESSAGE}: {nameof(message)}");
            }
            else if (message.GetType() == typeof(string) && string.IsNullOrEmpty(message.ToString()))
            {
                throw new ArgumentNullException($"{Kafka_Producer.KAFKA_WARN_NULLEMPTY_MESSAGE}: {nameof(message)}");
            }

            return true;
        }
    }
}
